﻿using Microsoft.Practices.EnterpriseLibrary.Data;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace WebServer.DBUtility
{

    public class SQLHelper
    {
        public static string ConnectionString = "ConnectionString";


        //public SQLHelper(string DB)
        //{
        //    //try
        //    //{
        //    //    if (!string.IsNullOrEmpty(DB))
        //    //    {
        //    //        ConnectionString = DB + "ConnectionString";
        //    //    }
        //    //}
        //    //catch { }
        //}
        public int ExecuteNonQuery(string sql)
        {
            Database db = DatabaseFactory.CreateDatabase(ConnectionString);
            return db.ExecuteNonQuery(CommandType.Text, sql);
        }

        public int ExecuteNonQuery(DbTransaction transaction, string sql)
        {
            Database db = DatabaseFactory.CreateDatabase(ConnectionString);
            return db.ExecuteNonQuery(transaction, CommandType.Text, sql);
        }

        public int ExecuteNonQuery(string sql, SqlParameter[] paras)
        {
            Database db = DatabaseFactory.CreateDatabase(ConnectionString);
            DbCommand command = db.GetSqlStringCommand(sql);
            command.Parameters.AddRange(paras);
            return db.ExecuteNonQuery(command);
        }

        public int ExecuteNonQuery(DbTransaction transaction, string sql, SqlParameter[] paras)
        {
            Database db = DatabaseFactory.CreateDatabase(ConnectionString);
            DbCommand command = db.GetSqlStringCommand(sql);
            command.Parameters.AddRange(paras);
            return db.ExecuteNonQuery(command, transaction);
        }

        public IDataReader ExecuteReader(string sql)
        {
            Database db = DatabaseFactory.CreateDatabase(ConnectionString);
            return db.ExecuteReader(CommandType.Text, sql);
        }

        public IDataReader ExecuteReader(DbTransaction transaction, string sql)
        {
            Database db = DatabaseFactory.CreateDatabase(ConnectionString);
            return db.ExecuteReader(transaction, CommandType.Text, sql);
        }

        public IDataReader ExecuteReader(string spName, params object[] parameterValues)
        {
            Database db = DatabaseFactory.CreateDatabase(ConnectionString);
            return db.ExecuteReader(spName, parameterValues);
        }

        public object ExecuteScalar(string sql)
        {
            Database db = DatabaseFactory.CreateDatabase(ConnectionString);
            return db.ExecuteScalar(CommandType.Text, sql);
        }


        public object ExecuteSPScalar(string spName, params object[] values)
        {
            Database db = DatabaseFactory.CreateDatabase(ConnectionString);
            return db.ExecuteScalar(spName, values);
        }

        public object ExecuteSPScalar(DbTransaction transaction, string spName, params object[] values)
        {
            if (transaction == null)
                return ExecuteSPScalar(spName, values);
            Database db = DatabaseFactory.CreateDatabase(ConnectionString);
            return db.ExecuteScalar(transaction, spName, values);
        }


        public object ExecuteScalar(DbTransaction transaction, string sql)
        {
            Database db = DatabaseFactory.CreateDatabase(ConnectionString);
            return db.ExecuteScalar(transaction, CommandType.Text, sql);
        }

        public object ExecuteScalar(DbCommand cmd)
        {
            Database db = DatabaseFactory.CreateDatabase(ConnectionString);
            return db.ExecuteScalar(cmd);
        }

        public object ExecuteScalar(DbTransaction transaction, DbCommand cmd)
        {
            Database db = DatabaseFactory.CreateDatabase(ConnectionString);
            return db.ExecuteScalar(cmd, transaction);
        }

        public object ExecuteScalar(string sql, SqlParameter[] paras)
        {
            return ExecuteScalar(null, sql, paras);
        }

        public object ExecuteScalar(DbTransaction transaction, string sql, SqlParameter[] paras)
        {
            Database db = DatabaseFactory.CreateDatabase(ConnectionString);
            DbCommand command = db.GetSqlStringCommand(sql);
            command.Parameters.AddRange(paras);
            if (transaction != null)
                return db.ExecuteScalar(command, transaction);
            else
                return db.ExecuteScalar(command);
        }

        public DbConnection CreateConnection()
        {
            Database db = DatabaseFactory.CreateDatabase(ConnectionString);
            return db.CreateConnection();
        }


    }
}
